package com.itheima.service;

import com.itheima.dao.OtherPersonDao;
import com.itheima.dao.PersonDao;
import com.itheima.domain.Person;
import com.itheima.domain.Student;

import java.util.Scanner;

public class StudentService extends PersonService {
    OtherPersonDao sd = new OtherPersonDao();
    Scanner sc = new Scanner(System.in);
    public void add() {
        while(true){
            String nextSid;
            System.out.println("请输入学号");
            while (true){
                nextSid = sc.next();
                int b= sd.isExist(nextSid);
                if(b>=0){
                    System.out.println("学号存在请重新输入：");
                }else{
                    break;
                }
            }
            System.out.println("请输入名称：");
            String nextName = sc.next();
            System.out.println("请输入年龄：");
            String nextAge = sc.next();
            System.out.println("请输入生日：");
            String nextBirthday = sc.next();
            Student s = new Student(nextSid,nextName,nextAge,nextBirthday);
            boolean b = sd.add(s);
            if(b){
                System.out.println("添加成功");
                break;
            }else{
                System.out.println("仓库满了，删除学生后添加");
            }
        }
    }
    public void select() {
        Person[] select = null;
        try{
            select =sd.select();
            if(select[0]==null){}
            System.out.println("学生的学号      姓名     年龄       生日");

        }catch (Exception e){
            System.out.println("查无信息，请添加！");
        }
        for (int i = 0; i < select.length; i++) {
            Person s = select[i];
            System.out.println(s.getSid()+"..."+s.getName()+"..."+s.getAge()+"..."+s.getBirthday());
        }
    }

    public void delete() {
        System.out.println("输入想要删除的身份码：");
        int indexSid = checkId();
        sd.delete(indexSid);
        System.out.println("删除成功！");
    }

    public void insert() {
        System.out.println("输入想要修改的身份码：");
        Person [] select = sd.select();
        int indexSid = checkId();
        Person student = select[indexSid];
        System.out.println("请输入新的名称");
        String name = sc.next();
        student.setName(name);
        System.out.println("请输入新的年龄");
        String age = sc.next();
        student.setAge(age);
        System.out.println("请输入新的生日");
        String birthday = sc.next();
        student.setBirthday(birthday);
        System.out.println("修改成功");
    }
}
